namespace SimpleWebServices.Web.UI.Html.Elements
{
    using System;
    using System.Xml;
    using System.Xml.Serialization;

    [XmlRoot("col")]
    public class Column : CellAlignment, IXmlSerializable
    {
        public string Span
        {
            get;
            set;
        }

        public string Width
        {
            get;
            set;
        }

        public override IElement Deserialize(string name, string type, string xml)
        {
            IElement result = null;

            if (null == type && "col".Equals(name, StringComparison.OrdinalIgnoreCase))
            {
                result = xml.Deserialize<Column>();
            }

            return result;
        }

        public override void ReadXml(XmlReader reader)
        {
            string value = reader.GetAttribute("span");
            if (!string.IsNullOrEmpty(value))
            {
                this.Span = value;
            }

            value = reader.GetAttribute("width");
            if (!string.IsNullOrEmpty(value))
            {
                this.Width = value;
            }

            base.ReadXml(reader);
        }

        public override void WriteXml(XmlWriter writer)
        {
            if (!string.IsNullOrEmpty(this.Span))
            {
                writer.WriteAttributeString("span", this.Span);
            }

            if (!string.IsNullOrEmpty(this.Width))
            {
                writer.WriteAttributeString("width", this.Width);
            }

            base.WriteXml(writer);
        }
    }
}